package com.mb.mp.service.impl;

import com.mb.common.config.CommonConfig;
import com.mb.common.utils.StringUtils;
import com.mb.mp.dao.MpProductInfoDOMapper;
import com.mb.mp.dao.RechargeCfgDOMapper;
import com.mb.mp.domain.MpProductInfoDO;
import com.mb.mp.domain.RechargeCfgDO;
import com.mb.mp.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品管理服务实现
 *
 * @author
 * @create 2018-09-19 下午3:34
 **/
@Service("productService")
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    private CommonConfig commonConfig;

    @Autowired
    private MpProductInfoDOMapper productInfoDOMapper;

    @Autowired
    private RechargeCfgDOMapper rechargeCfgDOMapper;

    @Override
    public List<MpProductInfoDO> queryList(Map<String, Object> map) {
        List<MpProductInfoDO> list = productInfoDOMapper.list(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return productInfoDOMapper.count(map);
    }


    @Override
    public int save(MpProductInfoDO productInfo) {
        productInfo.setHotUpdateTime(new Date());
        return productInfoDOMapper.insertSelective(productInfo);
    }


    @Override
    public int update(MpProductInfoDO productInfo) {
        try {
            MpProductInfoDO productInfoDO = productInfoDOMapper.selectByPrimaryKey(productInfo.getProductId());
            if(productInfoDO != null){
                productInfoDO.setDataStatus(1);
                BeanUtils.copyProperties(productInfoDO,productInfo);
                int isHot = productInfo.getIsHot() == null ? 0 : productInfo.getIsHot().intValue();
                if(isHot == 1){
                    productInfoDO.setHotUpdateTime(new Date());
                    return productInfoDOMapper.updateByPrimaryKeySelective(productInfoDO);
                }else{
                    productInfoDOMapper.updateByPrimaryKeySelective(productInfoDO);

                    MpProductInfoDO productInfoEntity = new MpProductInfoDO();
                    productInfoEntity.setProductId(productInfo.getProductId());
                    productInfoEntity.setHotUpdateTime(null);
                    return productInfoDOMapper.updateByPrimaryKeySelective(productInfoEntity);
                }

            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }

    @Override
    public int remove(Long productId) {
        return productInfoDOMapper.deleteByPrimaryKey(productId);
    }

    @Override
    public MpProductInfoDO getDtl(Long productId) {
        return productInfoDOMapper.selectByPrimaryKey(productId);
    }


    @Override
    public int updateRecharge(RechargeCfgDO rechargeCfgDO) {
        RechargeCfgDO entity = rechargeCfgDOMapper.selectByPrimaryKey(rechargeCfgDO.getId());
        if(entity != null){
            String wepayImgUrl = rechargeCfgDO.getWepayImgUrl();
            String alipayImgUrl = rechargeCfgDO.getAlipayImgUrl();

            wepayImgUrl = wepayImgUrl.replaceAll(commonConfig.getImgDomain(),"");
            alipayImgUrl = alipayImgUrl.replaceAll(commonConfig.getImgDomain(),"");

            entity.setWepayImgUrl(wepayImgUrl);
            entity.setAlipayImgUrl(alipayImgUrl);

            entity.setRechargeAmt(rechargeCfgDO.getRechargeAmt());
            entity.setLongTermRechargeAmt(rechargeCfgDO.getLongTermRechargeAmt());

            rechargeCfgDOMapper.updateByPrimaryKeySelective(entity);
        }
        return 0;
    }

    @Override
    public RechargeCfgDO getRechargeCfg(RechargeCfgDO rechargeCfgDO) {
        RechargeCfgDO entity = rechargeCfgDOMapper.selectByPrimaryKey(rechargeCfgDO.getId());
        if(entity != null){
            if(StringUtils.isNotEmpty(entity.getWepayImgUrl())){
                entity.setWepayImgUrl(commonConfig.getImgDomain() + entity.getWepayImgUrl());
            }

            if(StringUtils.isNotEmpty(entity.getAlipayImgUrl())){
                entity.setAlipayImgUrl(commonConfig.getImgDomain() + entity.getAlipayImgUrl());
            }
        }
        return entity;
    }
}


